package com.secondboot.controller;

import com.secondboot.entity.Article;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @Classname: ArticleController
 * @Author: bromide
 * @CreateTime: 2022/4/23--下午2:30
 * @Version: V1.0
 * @Description:
 */
@Controller
public class ArticleController {
    private static File articleDir;
    static {
        articleDir = new File("./article");
        if (!articleDir.exists()){
            articleDir.mkdirs();
        }
    }

    @RequestMapping("/myweb/writeArticle")
    public void writeArticle(String title, String author, String content, HttpServletResponse response){
        File articleFile = new File(articleDir,title+".obj");
        try (
            FileOutputStream fos = new FileOutputStream(articleFile);
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            )
        {
            Article article = new Article(title,author,content);
            oos.writeObject(article);
            response.sendRedirect("/article_success.html");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    @RequestMapping("/myweb/showAllArticle")
    public void showAllArticle(HttpServletRequest request, HttpServletResponse response){
        //将文件夹中所有文章对象读取到List中备用
        List<Article> articleList = new ArrayList<>();
        File[] files = articleDir.listFiles(file -> file.getName().endsWith(".obj"));
        for(File file : files){
            try(
                FileInputStream fis = new FileInputStream(file);
                ObjectInputStream ois = new ObjectInputStream(fis);
            )
            {
                Article article = (Article)ois.readObject();
                articleList.add(article);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }

        //设置返回值类型，如果写出后再设置将会乱码
        response.setContentType("text/html;charset=utf-8");

        //写出动态页面
        try(
            PrintWriter pw = response.getWriter();
        )
        {
            pw.println("<!DOCTYPE html>");
            pw.println("<html lang=\"en\">");
            pw.println("<head>");
            pw.println("<meta charset=\"UTF-8\">");
            pw.println("<title>文章列表</title>");
            pw.println("</head>");
            pw.println("<body>");
            pw.println("<center>");
            pw.println("<h1>文章列表</h1>");
            pw.println("<form action=\"/myweb/showAllArticle\" method=\"post\">");
            pw.println("<table border=\"1\">");
            pw.println("<tr>");
            pw.println("<td>标题</td>");
            pw.println("<td>作者</td>");
            pw.println("<td>操作</td>");
            pw.println("</tr>");
            for(Article article : articleList){
                pw.println("<tr>");
                pw.println("<td><a href=\"/myweb/showContent?title="+article.getTitle()+"\" style=\"color: black\">"+article.getTitle()+"</a></td>");
                pw.println("<td>李诞</td>");
                pw.println("<td><a href=\"/myweb/deleteArticle?title="+article.getTitle()+"\" style=\"color: red\">删除</a></td>");
                pw.println("</tr>");
            }
            pw.println("</table>");
            pw.println("</form>");
            pw.println("</center>");
            pw.println("</body>");
            pw.println("</html>");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("/myweb/deleteArticle")
    public void deleteArticle(String title,HttpServletResponse response){
        File articleFile = new File(articleDir,title+".obj");
        articleFile.delete();
        try {
            response.sendRedirect("/myweb/showAllArticle");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("/myweb/showContent")
    public void showContent(String title,HttpServletResponse response){
        //读取对应到文章内容
        File articleFile = new File(articleDir,title+".obj");
        String content="";
        try(
            FileInputStream fis = new FileInputStream(articleFile);
            ObjectInputStream ois = new ObjectInputStream(fis);
            )
        {
            Article article = (Article)ois.readObject();
            content = article.getContent();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        //设置反馈类型
        response.setContentType("text/html;charset=utf-8");

        //写动态页面
        try(
            PrintWriter pw = response.getWriter();
            )
        {
            pw.println("<!DOCTYPE html>");
            pw.println("<html lang=\"en\">");
            pw.println("<head>");
            pw.println("<meta charset=\"UTF-8\">");
            pw.println("<title>正文内容</title>");
            pw.println("</head>");
            pw.println("<body>");
            pw.println("<center>");
            pw.println("<h1>文章内容</h1>");
            pw.println("<form action=\"/myweb/showContent\" method=\"post\">");
            pw.println("<table border=\"1\">");
            pw.println("<tr>");
            pw.println("<td align=\"center\">正文内容</td>");
            pw.println("</tr>");
            pw.println("<tr>");
            pw.println("<td><textarea name=\"content\" cols=\"50\" rows=\"20\">"+content+"</textarea></td>");
            pw.println("</tr>");
            pw.println("</table>");
            pw.println(" </form>");
            pw.println("</center>");
            pw.println("</body>");
            pw.println("</html>");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
